package com.ding.dailyup.controller;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ding.dailyup.entity.CollectStockEntity;
import com.ding.dailyup.entity.response.CollectStockResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ding.dailyup.annotation.LoginUser;
import com.ding.dailyup.entity.JsonResult;
import com.ding.dailyup.entity.dto.StockDTO;
import com.ding.dailyup.entity.response.ListCollectStockResp;
import com.ding.dailyup.entity.response.DailyLoginReward;
import com.ding.dailyup.exception.ParamException;
import com.ding.dailyup.model.UserCollectStock;
import com.ding.dailyup.redis.RedisService;
import com.ding.dailyup.service.UserService;
import com.ding.dailyup.util.DateUtil;
import com.ding.dailyup.util.StockUtil;
import com.ding.dailyup.util.UserTokenUtil;
import com.google.common.collect.Lists;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@Api(tags = "用户相关")
@RequestMapping(value = "/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private RedisService redisService;

	@ApiOperation(value = "收藏股票,返回收藏股票的扣减积分， 为0 不显示扣减弹窗")
	@GetMapping(value = "/collect/stock")
	public JsonResult collectStock(
			@ApiParam(value = "股票代码") @RequestParam(value = "stockCode") String stockCode,
			@LoginUser Long userId) {
		int decrPoints = 0;
		if (UserTokenUtil.isLogin(userId)) {
			CollectStockEntity collectStockEntity = userService
					.collectStock(userId, stockCode);
			decrPoints = collectStockEntity.getDecrPoints();
			CollectStockResp collectStockResp = new CollectStockResp();
			collectStockResp.setDecrPoints(decrPoints);
			collectStockResp.setRecordId(collectStockEntity.getRecordId());
		}

		return JsonResult.succResult();
	}

	@ApiOperation(value = "取消收藏股票")
	@GetMapping(value = "/cancel/collect")
	public JsonResult cancelCollectStock(
			@ApiParam(value = "stockCode") @RequestParam(value = "stockCode") String stockCode,
			@LoginUser Long userId) {
		if (UserTokenUtil.isLogin(userId)) {
			userService.cancelCollectStock(userId, stockCode);
		}
		return JsonResult.succResult();
	}

	@ApiOperation("收藏股票列表")
	@GetMapping(value = "/collect/stocks")
	public JsonResult<List<ListCollectStockResp>> collectStocks(
			@LoginUser Long userId) {
		List<ListCollectStockResp> resp = Lists.newArrayList();
		if (UserTokenUtil.isLogin(userId)) {
			List<UserCollectStock> userCollectStocks = userService
					.listUserCollectStocks(userId);

			Map<String, StockDTO> rs = StockUtil
					.getStockPriceWithSimpleCode(userCollectStocks.stream()
							.map(UserCollectStock::getStockCode)
							.collect(Collectors.toList()));

			resp = userCollectStocks.stream().map(userCollectStock -> {
				ListCollectStockResp collectRs = new ListCollectStockResp();
				BeanUtil.copyProperties(userCollectStock, collectRs);
				if (CollectionUtil.isNotEmpty(rs) && ObjectUtil
						.isNotNull(rs.get(collectRs.getStockCode()))) {
					StockDTO stockDTO = rs.get(collectRs.getStockCode());
					collectRs.setLatest(stockDTO.getLatest());
				}
				return collectRs;
			}).sorted(Comparator.comparing(ListCollectStockResp::getId)
					.reversed()).collect(Collectors.toList());
		}

		return JsonResult.succResult(resp);
	}

	@ApiOperation(value = "对股票备注")
	@GetMapping(value = "/remark/stock")
	public JsonResult remarkStock(
			@ApiParam(value = "备注内容") @RequestParam(value = "remarkContext") String remarkContext,
			@ApiParam(value = "收藏记录的id") @RequestParam(value = "recordId") Long recordId,
			@LoginUser Long userId) {
		if (remarkContext.length() > 50) {
			throw new ParamException("长度不能超过50个字符!");
		}
		if (UserTokenUtil.isLogin(userId)) {
			userService.remarkStock(userId, recordId, remarkContext);
		}
		return JsonResult.succResult();
	}

	@ApiOperation(value = "是否是今天第一次登陆")
	@GetMapping(value = "/today/first/login")
	public JsonResult<DailyLoginReward> isTodayFirstLogin(
			@LoginUser Long userId) {
		boolean rs = false;
		Integer point = 0;
		if (UserTokenUtil.isLogin(userId)) {
			rs = redisService.dailyFirstLogin(
					DateUtil.formatYYYYMM(LocalDateTime.now()),
					String.valueOf(userId), System.currentTimeMillis());
			if (rs) {
				point = userService.dailyLoginReward(userId);
			}
		}
		return JsonResult.succResult(new DailyLoginReward(rs, point));
	}
}
